<template>
  <div>
    <!-- 搜索的布局 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="学员状态">
        <el-select v-model="formInline.status" placeholder="请选择">
          <el-option label="启用" value="1"></el-option>
          <el-option label="禁用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学生名称">
        <el-input
          v-model="formInline.user"
          placeholder="请输入学生关键字"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input
          v-model="formInline.phone"
          placeholder="请输入手机号"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="search">搜索</el-button>
        <el-button icon="el-icon-loading" @click="chong">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 展示数据的表格 -->
    <el-button type="primary" class="btn" @click="zeng">新增学员</el-button>
    <el-table
      :data="list"
      ref="multipleTable"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column
        label="学员名称"
        width="120"
        prop="nickname"
      ></el-table-column>
      <el-table-column fixed prop="data" label="头像" width="150">
        <template slot-scope="scope">
          <el-image
            style="width: 50px; height: 50px; border-radius: 50%"
            :src="scope.row.avatar"
            :preview-src-list="imglist"
            @click="imglist.push(scope.row.avatar)"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="手机号" width="180" prop="mobile">
      </el-table-column>
      <el-table-column label="状态" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 1">启用</span>
          <span v-if="scope.row.status == 0">禁用</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="180">
        <template slot-scope="scope">{{
          scope.row.created_at | filtime
        }}</template>
      </el-table-column>
      <el-table-column label="操作" width="160">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="del(scope.row.uid)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 删除弹出框 -->
    <el-dialog :visible.sync="centerDialogVisible" width="30%" center>
      <span>确定要删除吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="queding">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分页 -->
    <el-pagination
      @size-change="statusChage"
      @current-change="currentChange"
      :page-sizes="[2, 3, 5]"
      :current-page="page"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 新增学员弹出框 -->
    <el-dialog title="新增学员" :visible.sync="dialogFormVisible">
      <el-form :model="form" label-width="100px">
        <el-form-item label="学生名称:">
          <el-input
            placeholder="请输入学生名称"
            v-model="form.name"
            class="el-input"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="学生手机号:">
          <el-input
            placeholder="请输入学生手机号"
            v-model="form.pass"
            class="el-input"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="性别:" label-width="100px">
          <el-select
            v-model="form.region"
            placeholder="请选择"
            class="el-input"
          >
            <el-option label="男" value="1"></el-option>
            <el-option label="女" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期">
          <div class="block">
            <span class="demonstration"></span>
            <el-date-picker
              v-model="form.value1"
              type="datetime"
              placeholder="选择日期时间"
            >
            </el-date-picker>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="que">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { user, dels, xinzeng } from "../../api/api";
export default {
  data() {
    return {
      msg: "",
      dialogFormVisible: false,
      form: {
        name: "",
        pass: "",
        region: "",
        value1: "",
      },
      // 所有的数据
      list: [],
      page: 1,
      limit: 5,
      total: 0,
      // 使用双向数据绑定
      formInline: {
        user: "",
        phone: "",
        status: "",
        clickId: "",
      },
      // 删除静态框
      centerDialogVisible: false,
      // 点击查看大图
      imglist: [],
    };
  },
  // 进入页面请求所有数据
  mounted() {
    this.feng();
  },
  // 时间的过滤
  filters: {
    filtime(val) {
      return new Date(val * 1000).toLocaleString();
    },
  },
  computed: {},
  methods: {
    // 删除
    del(uid) {
      this.centerDialogVisible = true;
      this.clickId = uid;
    },
    // 确定删除
    queding() {
      this.centerDialogVisible = false;
      dels(this.clickId).then(() => {
        this.feng();
      });
    },
    //  每页分几条
    statusChage(limit) {
      this.limit = limit;
      this.feng();
    },
    // 上一页下一页
    currentChange(page) {
      this.page = page;
      this.feng();
    },
    //封装函数
    feng() {
      user({
        nickname: this.formInline.user,
        mobile: this.formInline.phone,
        status: this.formInline.status,
        limit: this.limit,
        page: this.page,
      }).then((res) => {
        this.list = res.data.data.list;
        this.total = res.data.data.total;
      });
    },
    //搜索
    search() {
      this.page = 1;
      this.feng();
    },
    // 重置
    chong() {
      this.formInline.user = "";
      this.formInline.phone = "";
      this.formInline.status = "";
      this.feng();
    },
    // 新增学员
    zeng() {
      this.dialogFormVisible = true;
    },
    // 确定新增学员
    que() {
      xinzeng({
        birthday: this.form.value1,
        nickname: this.form.name,
        mobile: this.form.pass,
        sex: this.form.region,
      }).then((res) => {
        this.msg = res.data.msg;
        this.feng();
      });
      this.$message({
        message: this.msg,
        type: "warning",
      });
      this.form.value1 = "";
      this.form.name = "";
      this.form.pass = "";
      this.form.region = "";
      this.dialogFormVisible = false;
    },
  },
};
</script>
<style scoped>
.btn {
  float: left;
}
.el-input {
  width: 200px;
  float: left;
  margin-left: 20px;
}
</style>